<script setup lang="ts">
import { computed, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ADMIN_MENU_ITEMS, PARTNER_MENU_ITEMS } from '@/constants/MENU_ITEMS'
import { useAppStore } from '@/stores/app'
import { useAuthStore } from '@/stores/auth'
import {
  ArrowDown,
  User,
  SwitchButton,
  Odometer,
  Setting,
  Bell,
  Star,
  Key,
  Upload,
  CreditCard,
  Promotion,
  UserFilled,
  Document,
  Money,
  DataAnalysis,
  List,
  VideoPlay,
  Film,
  TrophyBase,
  Present,
  Wallet,
  Picture,
  Expand,
  Fold
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const app = useAppStore()
const authStore = useAuthStore()

const isCollapse = ref(false)

// 根据用户角色返回对应的菜单
const filteredMenuItems = computed(() => {
  return authStore.isPartner ? PARTNER_MENU_ITEMS : ADMIN_MENU_ITEMS
})

const isDark = computed(() => app.dark)
const onToggleDark = (val: boolean) => app.toggleDark(val)

function handleSelect(index: string) {
  router.push(index)
}

async function handleLogout() {
  await authStore.logout()
  router.push('/login')
}

// 菜单图标映射
function getMenuIcon(path: string) {
  const iconMap: Record<string, any> = {
    '/dashboard': Odometer,
    '/notice': Bell,
    '/advertisement-management': Picture,
    '/payment': CreditCard,
    '/gift-management': Star,
    '/gift-send-management': Present,
    '/members': UserFilled,
    '/partners': User,
    '/partner-points': Wallet,
    '/live-management': VideoPlay,
    '/video-management': Film,
    '/ranking-management': TrophyBase,
    '/logs': List,
  }
  return iconMap[path] || Setting
}
</script>

<template>
  <el-container class="admin-layout">
    <el-header height="64px" class="header">
      <div class="header-left">
        <div class="brand">
          <div class="brand-icon">🎯</div>
          <span class="brand-text">라이브팬</span>
        </div>
        <el-button
          :icon="isCollapse ? Expand : Fold"
          text
          class="collapse-btn"
          @click="isCollapse = !isCollapse"
        />
      </div>
      <div class="header-actions">
        <el-tooltip content="테마 전환" placement="bottom">
          <el-switch
            :model-value="isDark"
            @change="onToggleDark"
            inline-prompt
            active-text="🌙"
            inactive-text="☀️"
            class="theme-switch"
          />
        </el-tooltip>
        <el-dropdown trigger="click" class="user-dropdown">
          <div class="user-info">
            <el-avatar size="small" class="user-avatar">
              {{ authStore.user.name?.charAt(0)?.toUpperCase() || 'U' }}
            </el-avatar>
            <div class="user-details">
              <span class="username">{{ authStore.user.name || authStore.user.loginId }}</span>
              <span class="user-role" :class="authStore.user.role">
                {{ authStore.user.role === 'admin' ? '관리자' : '파트너' }}
              </span>
            </div>
            <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>
                <el-icon><User /></el-icon>
                개인 정보
              </el-dropdown-item>
              <el-dropdown-item divided @click="handleLogout">
                <el-icon><SwitchButton /></el-icon>
                로그아웃
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>
    <el-container class="main-container">
      <el-aside :width="isCollapse ? '64px' : '240px'" class="aside">
        <div class="menu-container">
          <el-menu
            :default-active="route.path"
            :collapse="isCollapse"
            :collapse-transition="false"
            class="sidebar-menu"
            @select="handleSelect"
          >
            <el-menu-item
              v-for="item in filteredMenuItems"
              :key="item.path"
              :index="item.path"
              class="menu-item"
            >
              <el-icon class="menu-icon">
                <component :is="getMenuIcon(item.path)" />
              </el-icon>
              <span class="menu-title">{{ item.label }}</span>
            </el-menu-item>
          </el-menu>
        </div>
      </el-aside>
      <el-main class="content-main">
        <div class="content-wrapper">
          <router-view />
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<style scoped>
.admin-layout {
  height: 100vh;
  background: var(--el-bg-color-page);
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  background: #fff;
  border-bottom: 1px solid var(--el-border-color-light);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
  z-index: 1000;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 18px;
  font-weight: 600;
  color: var(--el-color-primary);
}

.brand-icon {
  font-size: 24px;
}

.brand-text {
  letter-spacing: 0.5px;
}

.collapse-btn {
  color: var(--el-text-color-regular);
  font-size: 16px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 16px;
}

.theme-switch {
  --el-switch-on-color: var(--el-color-primary);
}

.user-dropdown {
  cursor: pointer;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 6px;
  transition: background-color 0.2s;
}

.user-info:hover {
  background: var(--el-fill-color-light);
}

.user-details {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.username {
  font-size: 14px;
  color: var(--el-text-color-regular);
  font-weight: 500;
}

.user-role {
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 500;
}

.user-role.admin {
  background: #e3f2fd;
  color: #1976d2;
}

.user-role.partner {
  background: #f3e5f5;
  color: #7b1fa2;
}

.dropdown-icon {
  font-size: 12px;
  color: var(--el-text-color-placeholder);
  transition: transform 0.2s;
}

.main-container {
  height: calc(100vh - 64px);
}

.aside {
  background: #fff;
  border-right: 1px solid var(--el-border-color-lighter);
  transition: width 0.3s ease;
  /* 允许内部容器滚动 */
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.menu-container {
  height: 100%;
  padding: 16px 0;
  overflow-y: auto;
}

.sidebar-menu {
  border: none;
  background: transparent;
}

.menu-item {
  margin: 4px 12px;
  border-radius: 8px;
  transition: all 0.2s;
}

.menu-item:hover {
  background: var(--el-color-primary-light-9);
}

.menu-item.is-active {
  background: var(--el-color-primary-light-8);
  color: var(--el-color-primary);
}

.menu-icon {
  font-size: 18px;
  margin-right: 8px;
}

.menu-title {
  font-size: 14px;
  font-weight: 500;
}

.content-main {
  background: var(--el-bg-color-page);
  padding: 0;
  overflow: hidden;
}

.content-wrapper {
  height: 100%;
  padding: 24px;
  background: var(--el-bg-color-page);
  overflow-y: auto;
}

/* 暗黑模式适配 */
:root.dark .header {
  background: var(--el-bg-color);
  border-bottom-color: var(--el-border-color);
}

:root.dark .aside {
  background: var(--el-bg-color);
  border-right-color: var(--el-border-color);
}

:root.dark .menu-item:hover {
  background: var(--el-color-primary-dark-2);
}

:root.dark .menu-item.is-active {
  background: var(--el-color-primary-light-3);
}

:root.dark .user-info:hover {
  background: var(--el-fill-color);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header {
    padding: 0 16px;
  }

  .brand-text {
    display: none;
  }

  .content-wrapper {
    padding: 16px;
  }
}
</style>

